package cn.xshi.sys.service.impl;

import java.util.List;
import java.util.Map;
import cn.hutool.core.collection.CollectionUtil;
import cn.xshi.sys.dao.SysNotifyReceiverDao;
import cn.xshi.sys.form.SysNotifyForm;
import cn.xshi.sys.model.SysNotify;
import cn.xshi.sys.service.SysNotifyService;
import cn.xshi.sys.dao.SysNotifyDao;
import cn.xshi.sys.model.SysNotifyReceiver;
import org.springframework.stereotype.Service;
import cn.xshi.common.base.BaseService;
import cn.xshi.common.util.ExceptionUtil;
import javax.annotation.Resource;
/**
 * @Desc 通知
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
public class SysNotifyServiceImpl extends BaseService implements SysNotifyService {

	@Resource
	private SysNotifyDao sysNotifyDao;

	@Resource
	private SysNotifyReceiverDao sysNotifyReceiverDao;

	/**
	 * 初始化分页
	 * @param sysNotifyForm
	 * @return
	 */
	public List<SysNotify> getSysNotifyListByCondition(SysNotifyForm sysNotifyForm){
		try {
			return sysNotifyDao.getSysNotifyListByCondition(sysNotifyForm);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 查询对象
	 * @param id
	 * @return
	 */
	public SysNotify getSysNotifyById(String id){
		try {
			return sysNotifyDao.getSysNotifyById(id);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 插入对象
	 * @param sysNotify
	 * @return
	 */
	public int addSysNotify(SysNotify sysNotify){
		try {
			List<SysNotifyReceiver> notifyReceivers = sysNotify.getNotifyReceivers();
			//先插入主表
			sysNotify.setCreateId(getCurrentAccountId());
			sysNotify.setCreateTime(getDate());
			sysNotifyDao.addSysNotify(sysNotify);
			//操作子表
			if(CollectionUtil.isNotEmpty(notifyReceivers)){
				for(SysNotifyReceiver notifyReceiver: notifyReceivers){
					notifyReceiver.setNotifyId(sysNotify.getId());
					sysNotifyReceiverDao.addSysNotifyReceiver(notifyReceiver);
				}
			}
			return 1;
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/***
	 * 删除
	 * @param condition
	 * @return
	 */
	public int delSysNotify(Map<String, Object> condition){
		try {
			return sysNotifyDao.delSysNotify(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}
}
